
*** Load 
use "${altered_data}data_melde.dta", clear

*** Event Study window
local window_begin = 15
local window_end = 42



*** Date of School Closings 2021

gen date_school_closing = .
//tag 0 muss letzter Schultag sein. date_school_closing = datum letzter Schultag
replace date_school_closing = td(18Jun2021) if inlist(idbundesland, 13, 1)  // MV, SH
replace date_school_closing = td(23Jun2021) if inlist(idbundesland, 2,11,12) // HH, BE, BB
replace date_school_closing = td(02Jul2021) if idbundesland == 5 //NW
replace date_school_closing = td(16Jul2021) if inlist(idbundesland, 6, 7,10) // HE, RP, SL
replace date_school_closing = td(21Jul2021) if inlist(idbundesland, 3, 4, 15) // NI, HB, ST
replace date_school_closing = td(23Jul2021) if inlist(idbundesland, 14, 16) // SN, TH
replace date_school_closing = td(29Jul2021) if idbundesland == 9 // BY
replace date_school_closing = td(28Jul2021) if idbundesland == 8 // BW

format date_school_closing %td


*** Date of School Openings 2021

gen date_school_opening = .  
//tag 0 muss letzter Ferientag sein. date_school_opening = datum letzter Ferientag
replace date_school_opening = td(01Aug2021) if inlist(idbundesland, 13, 1) // MV, SH
replace date_school_opening = td(04Aug2021) if idbundesland == 2 // HH
replace date_school_opening = td(08Aug2021) if idbundesland == 11 //  BE
replace date_school_opening = td(08Aug2021) if idbundesland == 12 //  BB
replace date_school_opening = td(17Aug2021) if idbundesland == 5 //NW
replace date_school_opening = td(29Aug2021) if inlist(idbundesland, 6, 7, 10) // HE, RP, SL
replace date_school_opening = td(01Sep2021) if inlist(idbundesland, 3, 4, 15) // NI, HB, ST
replace date_school_opening = td(05Sep2021) if inlist(idbundesland, 14, 16) // SN, TH
replace date_school_opening = td(13Sep2021) if idbundesland == 9 // BY
replace date_school_opening = td(12Sep2021) if idbundesland == 8 // BW

format date_school_opening %td


*** Define group of BL with early school opening:
gen early_BL = inlist(idbundesland, 13, 2, 1, 11, 12, 5, 6, 7, 10) 

*** Treatment Indicator

gen treated = 0
replace treated = 1 if date >= date_school_opening

gen treated_c = 0
replace treated_c = 1 if date >= date_school_closing

*** Time Relative to treatment onset

gen rel_time = date - date_school_opening
gen rel_time_c = date - date_school_closing


recode rel_time (.=0) (-1000/-`window_begin' = -`window_begin') (`window_end'/1000 = `window_end')
recode rel_time_c (.=0) (-1000/-`window_begin' = -`window_begin') (`window_end'/1000 = `window_end')

char rel_time[omit] 0
char rel_time_c[omit] 0

*** make dummies
xi i.rel_time, pref(_T_)
xi i.rel_time_c, pref(_Tc_)

unab vars: _T_*
unab vars: _Tc_*

scalar number_T_dummies = `: word count `vars''
scalar number_T_dummies_c = `: word count `vars''

*** Merge mobility data


rename RS kreis_id
merge m:1 idbundesland date using "${altered_data}google.dta",  keep(match master) nogenerate

rename kreis_id RS


*** Cumulative Number of Cases and trip variables in the last 14 days within a county:

preserve
	keep if age == 0
	keep RS date cases_melde_total 
	
	tsset RS date
	foreach var of varlist cases_melde_total {
		tssmooth ma `var'cum14 = `var', window(14 0 0)
		replace `var'cum14 = `var'cum14 * 14
	}
	rename cases_melde_totalcum14 cases_cum14
	drop cases_melde_total
	tempfile cum_cases
	save `cum_cases'
restore
	
merge m:1 RS date using `cum_cases', nogen keep(master match)

*** Period of Observation:
save "${altered_data}event_study_fullperiod.dta", replace

keep if date >= td(01jun2021) & date <= td(02nov2021)

save "${altered_data}event_study.dta", replace


*** Prepare vaccination data:

import delimited "${orig_data}Aktuell_Deutschland_Landkreise_COVID-19-Impfungen.csv", clear 
drop if landkreisid_impfort == "u"
destring landkreisid_impfort, gen(RS)
drop landkreisid_impfort
sort RS impfdatum

keep if impfschutz == 2

//information on agegroups gets lost but age groups do not match age groups analysed
collapse (sum) anzahl, by(RS impfdatum)

gen date = date(impfdatum, "YMD")
format date %td

gen cum_cases = anzahl
replace  cum_cases = cum_cases[_n-1] + anzahl if RS == RS[_n-1]

xtset RS date
tsfill

replace cum_cases = cum_cases[_n-1] if cum_cases ==.
gen cum_cases_14_lag = cum_cases[_n-14] //crosses RS but doesnt matter because problematic cells get dropped

keep if date >= td(01jun2021) & date <= td(02nov2021)


drop impfdatum anzahl
drop if RS == 16056 | RS == 17000

merge 1:m RS date using "${altered_data}event_study.dta"  , nogenerate
save "${altered_data}event_study_vaccination.dta", replace


// Add the deaths by agegroup data
import delimited "${orig_data}todesfaelle.csv", clear
keep ags5 d2021* variable
reshape long d, i(ags5 variable) j(date1)
rename ags5 RS

//date
tostring date1, replace
gen date = date(date1, "YMD")
format date %td
drop date1
keep if date >= td(01jun2021) & date <= td(02nov2021)

sort RS date
keep if variable == "kr_tod_a0004" | variable == "kr_tod_a0514" | variable == "kr_tod_a1534" | variable == "kr_tod_a3559" | variable == "kr_tod_a6079" | variable == "kr_tod_a80"  | variable == "kr_tod_99"

rename variable age
replace age = "0" if age == "kr_tod_a0004"
replace age = "1" if age == "kr_tod_a0514"
replace age = "2" if age == "kr_tod_a1534" 
replace age = "3" if age == "kr_tod_a3559"
replace age = "4" if age == "kr_tod_a6079" | age == "kr_tod_a80" | age == "kr_tod_99"

destring age, replace 

tab d if age == 0
tab d if age == 1
tab d if age == 2
tab d if age == 3
tab d if age == 4

rename d deaths_by_age

collapse (sum) deaths_by_age, by(RS age date)
sort RS date age

bysort RS date: egen deaths_agg = sum(deaths_by_age)
merge 1:1 RS date age using "${altered_data}event_study_vaccination", nogenerate

//weighting of variable per 100k inhabitatnts
replace deaths_agg = deaths_agg/pop_total * 100000
replace deaths_by_age = deaths_by_age/pop_total * 100000

save "${altered_data}event_study_vaccination.dta", replace

























